package xyz.wanwanwan.www.hrm.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import xyz.wanwanwan.www.hrm.CourseDoc;
import xyz.wanwanwan.www.hrm.client.courseclient.RabbitmqClient;
import xyz.wanwanwan.www.hrm.config.RabbitmqConfig;
import xyz.wanwanwan.www.hrm.domain.Course;
import xyz.wanwanwan.www.hrm.domain.CourseDetail;
import xyz.wanwanwan.www.hrm.domain.CourseMarket;
import xyz.wanwanwan.www.hrm.dto.CourseDto;
import xyz.wanwanwan.www.hrm.mapper.CourseDetailMapper;
import xyz.wanwanwan.www.hrm.mapper.CourseMapper;
import xyz.wanwanwan.www.hrm.mapper.CourseMarketMapper;
import xyz.wanwanwan.www.hrm.query.CourseQuery;
import xyz.wanwanwan.www.hrm.searchcourse.client.SearchCourseClient;
import xyz.wanwanwan.www.hrm.service.ICourseService;
import xyz.wanwanwan.www.hrm.util.AjaxResult;
import xyz.wanwanwan.www.vo.email.SendEmailVo;
import xyz.wanwanwan.www.vo.sms.SendSMSVo;
import xyz.wanwanwan.www.vo.system.SendSystemVo;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Mr.Fu
 * @since 2020-09-11
 */
@Service
@Slf4j
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private SearchCourseClient searchCourseClient;
    @Value("${hrm.course.fastdfs}")
    private String fastDfsUrl;
    @Autowired
    private RabbitmqClient rabbitmqClient;
    @Override
    public void saveOrUpdate(CourseDto dto) {
        //获取课程
        Course course = dto.getCourse();
        //'课程状态，下线：0 ， 上线：1'
        course.setStatus(Course.COURSE_STATUS_OFFLINE);
        course.setTenantId(26L);
        course.setTenantName("源码时代");
        course.setUserId(42L);
        course.setUserName("yhptest1");
        course.setSaleCount(0);
        course.setViewCount(0);
        course.setCommentCount(0);
        baseMapper.insert(course);
        //获取课程明细
        CourseDetail courseDetail = dto.getCourseDetail();
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);
        //获取课程市场
        CourseMarket courseMarket = dto.getCourseMarket();
        courseMarket.setId(course.getId());
        courseMarketMapper.insert(courseMarket);
    }

    @Override
    public void onLineCourse(Long id) {
        //查询课程
        Course course = baseMapper.selectById(id);
        if(course==null||course.getStatus()!=Course.COURSE_STATUS_OFFLINE){
            throw new RuntimeException("课程已上线！！！！");
        }
        //将课程上线
        course.setStatus(Course.COURSE_STATUS_ONLINE);
        //保存课程
        baseMapper.updateById(course);
        //查询出课程明细
        CourseDetail courseDetail = courseDetailMapper.selectById(course.getId());
        //查询出课程商务
        CourseMarket courseMarket = courseMarketMapper.selectById(course.getId());
        //将课程等放进查询对象并放进es中
        CourseDoc courseDoc = new CourseDoc();
        BeanUtils.copyProperties(course,courseDoc);
        BeanUtils.copyProperties(courseDetail,courseDoc);
        BeanUtils.copyProperties(courseMarket,courseDoc);
        courseDoc.setOnlineTime(new Date());
        courseDoc.setPic(fastDfsUrl+course.getPic());
        courseDoc.setMyField(new StringBuilder().append(course.getName()).append(courseDetail.getDescription()).toString());
        //将课程存入es
        AjaxResult ajaxResult = searchCourseClient.saveDoc(courseDoc);
        if(!ajaxResult.isSuccess()){
            log.error("课程存入es失败了");
        }
        publishMessage(courseDoc.getName());
    }

    /**
     * 课程上线后发送消息
     */
    public void publishMessage(String courseName){
        System.out.println("================="+courseName);
        String message = "亲爱的用户，最新课程"+courseName+"已上线";
        SendSMSVo sendSMSVo = new SendSMSVo();
        sendSMSVo.setTel(1119199);
        sendSMSVo.setMessage(message);
        AjaxResult ajaxResult = rabbitmqClient.sendSMS(sendSMSVo);
        if(!ajaxResult.isSuccess()){
            log.error("发送短信失败了");
        }

        SendEmailVo sendEmailVo = new SendEmailVo("122@qq.com", message);
        AjaxResult ajaxResult1 = rabbitmqClient.sendEmail(sendEmailVo);
        if(!ajaxResult1.isSuccess()){
            log.error("发送邮件失败了");
        }

        SendSystemVo systemVo = new SendSystemVo(1L, "新课堂开课了", message);
        AjaxResult ajaxResult2 = rabbitmqClient.sendSystem(systemVo);
        if(!ajaxResult2.isSuccess()){
            log.error("发送站内信失败了");
        }

    }

    /**
     * 下线课程
     * @param id
     */
    @Override
    public void offLineCourse(Long id) {
        Course course = baseMapper.selectById(id);
        if(course==null||course.getStatus()!=Course.COURSE_STATUS_ONLINE){
            throw new RuntimeException("下线课程失败");
        }
        course.setStatus(Course.COURSE_STATUS_OFFLINE);
        baseMapper.updateById(course);
        searchCourseClient.delDoc(id);
    }
    /**
     * 前台条件查询
     * @param query
     * @return
     */
    @Override
    public AjaxResult queryCourses(CourseQuery query) {
       return searchCourseClient.queryCourse(query);
    }
}
